//
//  HZPeiziQueryView.m
//  Stock_Stategy
//
//  Created by MayerF on 2017/12/6.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZPeiziQueryView.h"
#import "HZPopMenuView.h"
#import "MYUpDownBtn.h"
#import "HZPeiziQueryCommonCell.h"
#import "HZPeiziQueryOwnCell.h"
#import "HZPeiziQueryDealCell.h"

@interface HZPeiziQueryView()<UITableViewDelegate, UITableViewDataSource, HZPopMenuViewDelegate>
@property (nonatomic, strong)HZPopMenuView    *pPopMenuView;
@property (nonatomic, strong)UITableView      *pTableView;
@property (nonatomic, strong)NSArray          *pDataList;

@property(nonatomic,strong)MYUpDownBtn    *pAlertInfo;
@property (nonatomic, strong)NSString *pInfo;
@end

@implementation HZPeiziQueryView

#pragma mark -
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        self.backgroundColor = kMYHexToRGB(0xf5f5f5, 1);
        [self commonInit];
    }
    return self;
}
#pragma mark -
#pragma mark private
- (void)commonInit{
    _pPopMenuView = [HZPopMenuView new];
    _pTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _pTableView.delegate = self;
    _pTableView.dataSource = self;
    _pTableView.tableFooterView = [UIView new];
    _pTableView.backgroundColor = [UIColor clearColor];
    
    [self p_initConstraints];
    
    [self p_setPopMenu];
    _pPopMenuView.hidden = YES;
    _pPopMenuView.delegate = self;
}

- (void)p_initConstraints
{
    [self addSubview:_pPopMenuView];
    [self addSubview:_pTableView];
    [_pTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self);
        make.top.mas_equalTo(self);
    }];
}
- (void)setNQueryType:(kQueryType)nQueryType
{
    _nQueryType = nQueryType;
    switch (nQueryType) {
        case kQuery_entrust_Type:
        case kQuery_deal_Type:
        {
            _pPopMenuView.hidden = NO;
            [_pPopMenuView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self);
                make.left.mas_equalTo(self);
                make.right.mas_equalTo(self);
                make.height.mas_equalTo(45);
            }];
            [_pTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_equalTo(self);
                make.top.mas_equalTo(_pPopMenuView.mas_bottom);
            }];
            if (nQueryType == kQuery_entrust_Type) {
                _pInfo = @"暂无数据";
            }else {
                _pInfo = @"暂无数据";
            }
            
            NSArray *pTitleArray = @[@"交易方向",@"委托日期"];
            if(_nQueryType == kQuery_deal_Type){
                pTitleArray = @[@"交易方向",@"成交日期"];
            }
            _pPopMenuView.titleArray = pTitleArray;
            [_pPopMenuView updateTitle:@"当天" index:1];
        }
            break;
        case kQuery_cancel_Type:
            _pInfo = @"暂无数据";
            break;
        case kQuery_own_Type:
            _pInfo = @"暂无数据";
            break;
            
        default:
            break;
    }
}

- (void)updateNewData:(NSArray *)data
{
    if (data && data.count) {
        _pAlertInfo.hidden = YES;
    }else {
        [self p_loadAlertInfo];
    }
    _pDataList = data;
    [_pTableView reloadData];
    if (_pDataList.count > 0) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [_pTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
}
- (void)p_loadAlertInfo{
    if (!_pAlertInfo) {
        _pAlertInfo = [MYUpDownBtn new];
        [_pAlertInfo setImage:[UIImage imageNamed:@"empty"] forState:UIControlStateNormal];
        [_pAlertInfo setImage:[UIImage imageNamed:@"empty"] forState:UIControlStateHighlighted];
        _pAlertInfo.imageView.contentMode = UIViewContentModeCenter;
        [_pAlertInfo setTitle:@"暂无策略" forState:UIControlStateNormal];
        [_pAlertInfo setTitleColor:kMYHexToRGB(0x888888, 1) forState:UIControlStateNormal];
        _pAlertInfo.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_pAlertInfo];
        [_pAlertInfo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self);
            make.left.mas_equalTo(self);
            make.right.mas_equalTo(self);
            make.height.mas_equalTo(70);
        }];
    }
    _pAlertInfo.hidden = NO;
    [_pAlertInfo setTitle:_pInfo forState:UIControlStateNormal];
}

- (void)p_setPopMenu
{
    //初始化控件内容
    NSMutableArray *pContentArray = [NSMutableArray arrayWithCapacity:2];
    NSMutableArray *pTempDate = [NSMutableArray array];
    NSMutableArray *pMoneyType = [NSMutableArray array];
    
    NSArray *moneyArray = [NSArray arrayWithObjects:
                           @"全部",
                           @"买入",
                           @"卖出",
                           nil];
    NSArray *dateArray = [NSArray arrayWithObjects:
                          @"全部",
                          @"当天",
                          @"7天之内",
                          @"15天之内",
                          nil];
    
    for (NSString *title in dateArray) {
        HZPopMenuModel *model = [HZPopMenuModel new];
        model.model_name = title;
        [pTempDate addObject:model];
        if ([title isEqualToString:@"当天"]) {
            model.isShowImage = YES;
        }
    }
    for (NSString *title in moneyArray) {
        HZPopMenuModel *model = [HZPopMenuModel new];
        model.model_name = title;
        [pMoneyType addObject:model];
        if ([title isEqualToString:@"全部"]) {
            model.isShowImage = YES;
        }
    }
    
    [pContentArray addObject:pMoneyType];
    [pContentArray addObject:pTempDate];
    _pPopMenuView.contentArray = pContentArray;
}

#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _pDataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    HZPeiziTradeStockListModel *model = _pDataList[indexPath.row];
    
    UITableViewCell *cell = nil;
    if (_nQueryType == kQuery_cancel_Type || _nQueryType == kQuery_entrust_Type) {
        HZPeiziCancelOrderListModel *model = _pDataList[indexPath.row];
        cell = [HZPeiziQueryCommonCell cellTableView:tableView indexPath:indexPath cellModel:model];
    }else if (_nQueryType == kQuery_own_Type) {
        HZPeiziTradeStockListModel *model = _pDataList[indexPath.row];
        cell = [HZPeiziQueryOwnCell cellTableView:tableView indexPath:indexPath cellModel:model];
    }else {
        HZPeiziChengjiaoListModel *model = _pDataList[indexPath.row];
        cell = [HZPeiziQueryDealCell cellTableView:tableView indexPath:indexPath cellModel:model];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark -
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_nQueryType == kQuery_cancel_Type || _nQueryType == kQuery_entrust_Type || _nQueryType == kQuery_own_Type) {
        return 240;
    }
    return 200;
}
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//
//    HZStategyListModel *model = _pDataList[indexPath.row];
//
//    LogicRouterBlock excuteBlock = [self.logicManager matchLogicBlock:kLogicRouterExcute_clickList];
//    if (excuteBlock) {
//        excuteBlock(nil, model);
//    }
//}
#pragma mark -
#pragma mark HZPopMenuViewDelegate
- (void)popMenuViewDidSelectedRowWithContent:(NSArray *)content{
    
    NSMutableArray *pTempArray = [NSMutableArray arrayWithCapacity:5];
    NSString *pTime,*pStart, *pEnd, *pMoneyType;
    pMoneyType = content[0];
    pTime = content[1];
    
    pStart = @"";
    pEnd = @"";
    if ([pMoneyType isEqualToString:@"交易方向"]) {
        pMoneyType = @"";
    }else if ([pMoneyType isEqualToString:@"卖出"]) {
        pMoneyType = @"2";
    }else if ([pMoneyType isEqualToString:@"买入"]) {
        pMoneyType = @"1";
    }
    if ([pTime isEqualToString:@"委托日期"] || [pTime isEqualToString:@"成交日期"]) {
        [self commonToolYMDTimeWithStart:&pStart end:&pEnd endDay:0 rangeDay:0];
        pStart = @"20171102";
    }else if ([pTime isEqualToString:@"7天之内"]) {
        [self commonToolYMDTimeWithStart:&pStart end:&pEnd endDay:0 rangeDay:7];
    }else if ([pTime isEqualToString:@"15天之内"]) {
        [self commonToolYMDTimeWithStart:&pStart end:&pEnd endDay:0 rangeDay:15];
    }
    
    [pTempArray addObject:pStart];
    [pTempArray addObject:pEnd];
    [pTempArray addObject:pMoneyType];
    
    if (_nQueryType == kQuery_entrust_Type) {
        LogicRouterBlock excuteBlock = [self.logicManager matchLogicBlock:kLogicRouterPeiziWeiTuoFind];
        if (excuteBlock) {
            excuteBlock(nil, pTempArray);
        }
    }else if(_nQueryType == kQuery_deal_Type){
        LogicRouterBlock excuteBlock = [self.logicManager matchLogicBlock:kLogicRouterPeiziDealFind];
        if (excuteBlock) {
            excuteBlock(nil, pTempArray);
        }
    }
}

- (void)commonToolYMDTimeWithStart:(NSString **)start
                               end:(NSString **)end
                            endDay:(int)endDay
                          rangeDay:(int)rangeDay
{
    double dCurrentTime = [[NSDate date] timeIntervalSince1970];//当天日期
    double dEndTime = dCurrentTime - endDay*24*60*60;//结束时间
    double dStartTime = dEndTime - (rangeDay-1)*24*60*60;//开始时间
    *end = [self commonToolYMDTimeStrWithTimeInterval:dEndTime];
    *start = [self commonToolYMDTimeStrWithTimeInterval:dStartTime];
}

- (NSString *)commonToolYMDTimeStrWithTimeInterval:(int)timeInterval
{
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMdd"];
    NSString *timeStrT = [formatter stringFromDate:time];
    return timeStrT;
}

@end
